/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.marifnst.itd.preprocessing;

import com.sun.rowset.WebRowSetImpl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.sql.rowset.WebRowSet;

/**
 *
 * @author arifnasution
 */
public class Stemming {

    private static Stemming stemming;
    private Connection connection;
    private Statement statement;
    private WebRowSet webRowSet;
    public String[] imbuhanAwalan = {"di",
        "me", "men", "meng", "meny", "mem",
        "ber",
        "pe",
        "per",
        "ter",
        "ke"};

    public void openConnection() {
        try {
            // windows
//            String url = "jdbc:hsqldb:file:/" + System.getProperty("user.dir").substring(3) + "/database/db_conf";
            // linux
            String url = "jdbc:hsqldb:file:/" + System.getProperty("user.dir") + "/database/db_conf";
            connection = DriverManager.getConnection(url, "sa", "");
            statement = connection.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(Stemming.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void closeConnection() {
        try {
            connection.close();
        } catch (SQLException ex) {
            Logger.getLogger(Stemming.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static Stemming getInstance() {
        if (stemming == null) {
            stemming = new Stemming();
        }
        return stemming;
    }

    public boolean checkFirstWord(String firstChar) {
        String stringPattern = "[0-9]";
        Pattern pattern = Pattern.compile(stringPattern);
        Matcher matcher = pattern.matcher(firstChar);
        return matcher.matches();
    }

    public String getSourceString(char key) {
        return "SET TABLE data_" + key + " SOURCE \"data_" + key + ".csv;ignore_first=true\"";
    }

    public boolean stemmingProcess(String word) {
        boolean result = false;
        if (!checkFirstWord(word.substring(0, 1))) {
            try {
                openConnection();
                statement.execute(getSourceString(word.charAt(0)));

                webRowSet = new WebRowSetImpl();
                webRowSet.setCommand("select * from data_" + word.charAt(0) + " where name='" + word + "'");
                webRowSet.execute(connection);
                result = webRowSet.next();
                if (!result) {
                    webRowSet.setCommand("insert into data_" + word.charAt(0) + " (name) values ('" + word + "')");
                    webRowSet.execute(connection);
                }
                closeConnection();
            } catch (SQLException ex) {
                Logger.getLogger(Stemming.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return result;
    }
}
